package top.infopub.mgr.bpa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.infopub.dto.bpa.OtherContractsReviewDto;
import top.infopub.enums.mkt.ProjectFileTypeEnum;
import top.infopub.mgr.bpa.dao.OtherContractsReviewDao;
import top.infopub.mgr.bpa.domain.OtherConLedgerDomain;
import top.infopub.mgr.bpa.domain.OtherConReviewDomain;
import top.infopub.mgr.bpa.service.OtherContractsReviewService;
import top.infopub.mgr.bpa.vo.ContractManageVo;
import top.infopub.mgr.bpa.vo.OtherConReviewVo;
import top.infopub.mgr.mkt.dao.ProjectAttachmentDao;
import top.infopub.model.bpa.*;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import javax.annotation.PostConstruct;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

//import static top.infopub.mgr.bpa.service.impl.OtherContractsReplenishServiceImpl.buildTree;

/**
 * <p>
 * 其他合同评审 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-06-15
 */
@Service
public class OtherContractsReviewServiceImpl implements OtherContractsReviewService {

    private static OtherContractsReviewServiceImpl otherContractsReviewService;

    @Autowired
    private BpaContractBpaReviewServiceImpl bpaReviewService;
    @Autowired
    private ProjectAttachmentDao projectAttachmentDao;
    @Autowired
    private OtherContractsReviewDao  otherContractsReviewDao;
    @PostConstruct
    public void init() {
        otherContractsReviewService = this;
        otherContractsReviewService.otherContractsReviewDao = this.otherContractsReviewDao;
    }


    @Override
    public Page<OtherConReviewDomain> selectByDomainWhere(Page<OtherConReviewDomain> page, OtherConReviewVo otherConReviewVo) throws ParseException {
        //处理时间范围
        if (!StringUtils.isEmpty(otherConReviewVo.getDates())) {
            String[] splitTime = otherConReviewVo.getDates().split("~");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            otherConReviewVo.setStartDate(format.parse(splitTime[0]));
            otherConReviewVo.setEndDate(format.parse(splitTime[1]));
        }
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<OtherConReviewDomain>) otherContractsReviewDao.selectByDomainWhere(otherConReviewVo);
    }


    @Override
    public Page<OtherConLedgerDomain> selectByLedger(Page<OtherConLedgerDomain> page, ContractManageVo contractManageVo) throws ParseException {
        //处理时间范围
        if (!StringUtils.isEmpty(contractManageVo.getDates())) {
            String[] splitTime = contractManageVo.getDates().split("~");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            contractManageVo.setStartDate(format.parse(splitTime[0]));
            contractManageVo.setEndDate(format.parse(splitTime[1]));
        }
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<OtherConLedgerDomain>) otherContractsReviewDao.selectByLedger(contractManageVo);
    }

    @Override
    public OtherContractsReviewDto queryById(String id) {
        OtherContractsReviewDto dto = new OtherContractsReviewDto();
        //根据id查询主表数据
        OtherContractsReview otherContractsReview = otherContractsReviewDao.findById(id);
        //根据主表数据的proCode查询 项目基本信息
        String proCode = otherContractsReview.getProCode();
        if (!StringUtils.isEmpty(proCode)) {
            Basic basic = otherContractsReviewDao.findByProCode(proCode);
            String projectLeader = otherContractsReview.getProjectLeader();
            if (!StringUtils.isEmpty(projectLeader)){
                basic.setProjectLeader(projectLeader);
            }
            dto.setBasic(basic);
        }
        //
        if (otherContractsReview != null) {
            dto.setOtherContractsReview(otherContractsReview);
        }
        //根据主表数据的id查询 项目代付费信息 类型 1合同评审页面  2补充协议页面
        if (StringUtils.isNoneBlank(id)) {
            //合同清单信息
            List<ProjectAgencyPayment> list = otherContractsReviewDao.selectProjectAgencyPayment(id, "1");
            dto.setProjectAgencyPaymentList(list);
            /*List<ProjectAgencyPayment> allList = otherContractsReviewDao.selectByOtherContracts("1");
            List<ProjectAgencyPayment> tree = buildTree(allList, id);
            dto.setProjectAgencyPaymentList(tree);*/
        }
        //根据主表数据的id查询 费用清单信息 类型 1合同评审页面  2补充协议页面
        if (otherContractsReview != null) {
            List<ExpenseInvoice> expenseInvoiceList = otherContractsReviewDao.selectInvoiceByOtherContractsId(id, "1");
            dto.setExpenseInvoiceList(expenseInvoiceList);
        }
        //附件信息
        List<ProjectAttachment> attachmentList = projectAttachmentDao.queryByWhere(otherContractsReview.getProCode(), ProjectFileTypeEnum.FILE_TYPE103.getCode(), null);
        if (attachmentList.size() > 0 && attachmentList != null) {
            String[] fileAtts = bpaReviewService.fileAttaListToArray(attachmentList);
            dto.setFileAtt(fileAtts);
        }
        return dto;
    }

    /*项目下拉框*/
    @Override
    public List<Map<String, String>> projectDropDownBox() {
        return otherContractsReviewDao.projectDropDownBox();
    }

    //根据procode查询项目基本信息
    @Override
    public Basic projectBasic(String proCode) {
        return otherContractsReviewDao.findByProCode(proCode);
    }

    @Override
    public int save(OtherContractsReviewDto otherContractsReviewDto, List<ProjectAttachment> attachmentList) {
        OtherContractsReview othercontractsReview = otherContractsReviewDto.getOtherContractsReview();
        //登录人员信息
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        if (othercontractsReview != null) {
            if (attachmentList != null && attachmentList.size() > 0) {
                //批量插入附件信息
                projectAttachmentDao.deleteByWhere(othercontractsReview.getProCode(), ProjectFileTypeEnum.FILE_TYPE103.getCode(), null);
                for (ProjectAttachment attachment : attachmentList) {
                    attachment.setProCode(othercontractsReview.getProCode());
                }
                projectAttachmentDao.insertBatch(attachmentList);
            }
            String id = othercontractsReview.getId();
            if (StringUtils.isEmpty(id)) {
                //第一次 保存
                othercontractsReview.setAddBy(vo.getId());
                othercontractsReview.setAddTime(new Date());
                othercontractsReview.setProcessStatus("0");
                //保存主表数据
                otherContractsReviewDao.insertOtherContractsReview(othercontractsReview);
                //保存费用清单信息表
                List<ExpenseInvoice> expenseInvoiceList = otherContractsReviewDto.getExpenseInvoiceList();
                for (ExpenseInvoice expenseInvoice : expenseInvoiceList) {
                    expenseInvoice.setType("1");
                    expenseInvoice.setAddTime(new Date());
                    expenseInvoice.setMasterId(othercontractsReview.getId());
                    otherContractsReviewDao.insertExpenseInvoice(expenseInvoice);
                }
                //保存子项数据 类型 1是其他合同评审 2是其他合同补充协议
               /* List<ProjectAgencyPayment> projectAgencyPaymentList = otherContractsReviewDto.getProjectAgencyPaymentList();*/
                /*saveAndUpdate(projectAgencyPaymentList, othercontractsReview.getId(), "1");*/
            } else {
                //修改
                othercontractsReview.setUptBy(vo.getId());
                othercontractsReview.setUptTime(new Date());
                otherContractsReviewDao.updateOtherContractsReview(othercontractsReview);
                //保存费用清单信息表
                List<ExpenseInvoice> expenseInvoiceList = otherContractsReviewDto.getExpenseInvoiceList();
                for (ExpenseInvoice expenseInvoice : expenseInvoiceList) {
                    if (StringUtils.isEmpty(expenseInvoice.getId())) {
                        //保存
                        expenseInvoice.setType("1");
                        expenseInvoice.setAddTime(new Date());
                        expenseInvoice.setMasterId(id);
                        otherContractsReviewDao.insertExpenseInvoice(expenseInvoice);
                    } else {
                        //修改
                        expenseInvoice.setType("1");
                        otherContractsReviewDao.updateExpenseInvoice(expenseInvoice);
                    }

                }
                //保存 1是其他合同评审 2是其他合同补充协议
                List<ProjectAgencyPayment> projectAgencyPaymentList = otherContractsReviewDto.getProjectAgencyPaymentList();
                /*saveAndUpdate(projectAgencyPaymentList, id, "1");*/
                //删除合同清单录入数据
                otherContractsReviewDao.deleteProjectAgencyPayment(id,"1");
                //保存合同清单录入数据
                int seq2 = 0;
                for (ProjectAgencyPayment payment : projectAgencyPaymentList) {
                    payment.setType("1");
                    payment.setContractId(id);
                    payment.setSeq(seq2++);
                }
                if (projectAgencyPaymentList!=null && projectAgencyPaymentList.size()>0){
                    otherContractsReviewDao.insertProjectAgencyPayment(projectAgencyPaymentList);
                }

            }
        }
        return 1;
    }

    @Override
    public OtherContractsReview findById(String id) {
        return otherContractsReviewDao.findById(id);
    }

    @Override
    public void deleteGenerationPay(String id) {
        otherContractsReviewDao.deleteGenerationPay(id);
    }

    @Override
    public List<Map<String, String>> companyNameDropDownBox(String types) {
        return otherContractsReviewDao.companyNameDropDownBox(types);
    }

   /* public static void saveAndUpdate(List<ProjectAgencyPayment> projectAgencyPaymentList, String sguid, String type) {
        for (ProjectAgencyPayment payment : projectAgencyPaymentList) {
            saveUpdate(payment, sguid, "1", type);
            //第二级
            List<ProjectAgencyPayment> projectAgencyPaymentList2 = payment.getProjectAgencyPaymentList();
            for (ProjectAgencyPayment payment2 : projectAgencyPaymentList2) {
                saveUpdate(payment2, payment.getId(), "2", type);
                ////第三级
                List<ProjectAgencyPayment> projectAgencyPaymentList3 = payment2.getProjectAgencyPaymentList();
                for (ProjectAgencyPayment payment3 : projectAgencyPaymentList3) {
                    saveUpdate(payment3, payment2.getId(), "3", type);
                }
            }
        }
    }*/
    /*public static void saveUpdate(ProjectAgencyPayment payment, String sguid, String level, String type) {
        if (StringUtils.isEmpty(payment.getId())) {
            //保存
            payment.setAddTime(new Date());
            payment.setHierarchy(level);
            payment.setType(type);
            payment.setMasterId(sguid);
            otherContractsReviewService.otherContractsReviewDao.saveProjectAgencyPayment(payment);
        } else {
            //修改
            otherContractsReviewService.otherContractsReviewDao.updateProjectAgencyPayment(payment);
        }
    }*/
}
